package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.constant.CacheKey;
import cc.yugu.yhdw.common.domain.basic.UserInfo;
import com.whalin.MemCached.MemCachedClient;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
 * 用户信息切面类
 *
 * @Date:Created in 2018/7/5 15:59
 * @Author:
 * @Modified By: zjn 2018/7/5 15:59
 */
@Component
@Aspect
public class UserServiceAspect {

    @Autowired
    MemCachedClient memCachedClient;

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.UserService.update(..))")
    private void update() {}

    @Pointcut("execution(* cc.yugu.yhdw.webserver.service.basic.UserService.delete(..))")
    public void delete(){}
    /**
    * 删除和更新
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  16:00
    */
    @Around("update()")
    public Object updateOrDelete(ProceedingJoinPoint joinPoint) throws Throwable {
        UserInfo userInfo = (UserInfo) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_USER_INFO, userInfo.getId());
        Object result = joinPoint.proceed();
        memCachedClient.delete(key);
        return result;
    }
    /**
    * 删除
    *
    * @Param: [joinPoint]
    * @Return: java.lang.Object
    * @Author By: zjn 2018/7/5  16:00
    */
    @Around("delete()")
    public Object delete(ProceedingJoinPoint joinPoint) throws Throwable {
        Integer id = (Integer) joinPoint.getArgs()[0];
        String key = CacheKey.key(CacheKey.K_ID_V_USER_INFO, id);
        Object result = joinPoint.proceed();
        memCachedClient.delete(key);
        return result;
    }
}
